package com.tianbo.controller;

import com.tianbo.common.ModelAndPage;
import com.tianbo.model.TOlapDataset;
import com.tianbo.service.OlapColumnService;
import com.tianbo.service.OlapDataSetService;
import com.tianbo.util.dao.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * Created by Mr.Xu on 2017/9/12.
 */
@Controller
@RequestMapping("/olapdata")
public class OlapDataSetController {
    @Autowired
    private OlapDataSetService olapDataSetService;
    @Autowired
    private OlapColumnService olapColumnService;
   @RequestMapping("/showlist")
    public ModelAndView findOlapDataSet(@ModelAttribute Page page) throws Exception {
        int pagelenth=10;
        int start=0;
       if(page.getBegin()>=0){start=page.getBegin();}
       if(page.getLength()>0){pagelenth=page.getLength();}
        ModelAndView modelAndView = new ModelAndView();
       ModelAndPage modelAndPage=new ModelAndPage();

        modelAndPage= olapDataSetService.findOlapDataSet(start,pagelenth);
        modelAndView.addObject("modelAndPage", modelAndPage);
        modelAndView.setViewName("olapdata/list");
        return modelAndView;
    }
    @RequestMapping("/addDataset")
    public ModelAndView addOlapDataset(HttpServletRequest request,TOlapDataset olapDataset) throws Exception {
        String datasetname=request.getParameter("datasetname");
        String viewname=request.getParameter("viewname");
        String active=request.getParameter("active");
        olapDataset.setDatasetname(datasetname);
        olapDataset.setViewname(viewname);
        olapDataset.setActive(active);
        olapDataSetService.addOlapDataSet(olapDataset);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("redirect:/olapdata/showlist");
        return modelAndView;
    }
    @RequestMapping("/deleteDataset")
    public String deleteDataset(HttpServletRequest request, BigDecimal datasetid){
        String id=request.getParameter("datasetid");
        BigDecimal bd=new BigDecimal(id);
        int result = olapColumnService.deleteOlapColumn(bd);
        if(result>0){
            olapDataSetService.deleteOlapDataset(bd);
            return "redirect:/olapdata/showlist";
        }else {
            return "500";
        }

    }
    @RequestMapping("/findByid")
    public ModelAndView findByid(HttpServletRequest request,BigDecimal datasetid)throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        String id=request.getParameter("datasetid");
        TOlapDataset olapDataset=olapDataSetService.findByid(new BigDecimal(id));
        modelAndView.addObject("olapDataset",olapDataset);
        modelAndView.setViewName("olapdata/edit");
        return modelAndView;
    }
    @RequestMapping("/updateDateset")
    public String updateDateset(HttpServletRequest request,TOlapDataset olapDataset){
        String datasetid=request.getParameter("datasetid");
        String datasetname=request.getParameter("datasetname");
        String viewname=request.getParameter("viewname");
        String active=request.getParameter("active");
        olapDataset.setDatasetid(new BigDecimal(datasetid));
        olapDataset.setDatasetname(datasetname);
        olapDataset.setViewname(viewname);
        olapDataset.setActive(active);
        olapDataSetService.updateOlapDataset(olapDataset);
        return "redirect:/olapdata/showlist";
    }
    @RequestMapping("list")
    public String list() {
        return "olapdata/list";
    }
}
